package com.hmyd.hmyd_android.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.adapter.ExchangeRecordAdapter;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.ExchangeRecord;
import com.hmyd.hmyd_android.dto.Page;
import com.hmyd.hmyd_android.view.MyListView;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

/**
 * 兑换记录
 * 
 * @author wangk
 * 
 */
public class ConvertRecordsFragment extends BasicFragmentList {

	private View rootView;
	private MyListView mLvRecords;
	private ScrollView mScrollView;
	private ExchangeRecordAdapter mAdapter;
	private List<ExchangeRecord> datas = new ArrayList<ExchangeRecord>();
	private final int REFRESH = 1;
	private final int LOADMORE = 2;
//	private String id;
	protected boolean hasMore = false;
	private String cursorId;

	public ConvertRecordsFragment(){}
	
	/*public ConvertRecordsFragment(String id) {
		this.id = id;
	}*/

	@Override
	public View onCreateView(LayoutInflater inflater,  ViewGroup container,
			 Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_goods_record, null);
		return rootView;
	}

	@SuppressLint("ClickableViewAccessibility")
	@Override
	public void onActivityCreated( Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mLvRecords = (MyListView) rootView.findViewById(R.id.record);
		mScrollView = (ScrollView) rootView.findViewById(R.id.scrollview);
		mScrollView.setOnTouchListener(new TouchListenerImpl());

		mAdapter = new ExchangeRecordAdapter(getActivity(), datas);
		mLvRecords.setAdapter(mAdapter);

	}

	@Override
	public void onResume() {
		super.onResume();
		RequestParams params = new RequestParams();
		params.addBodyParameter("itemId", getArguments().getString("itemId"));
		onRequest(REFRESH, UrlConstant.EXCHANGE_RECORD, HttpMethod.GET, params);
	}

	@Override
	public void onPostSuccess(int postId, String result) {
		super.onPostSuccess(postId, result);
		String res = null;
		String succesString = null;
		String errmsg = null;
		JSONObject jsonObject = null;
		try {
			jsonObject = new JSONObject(result);
			succesString = jsonObject.getString("suc");
		} catch (JSONException e) {
			e.printStackTrace();
		}

		if (succesString.equals("1012")) {
			try {
				errmsg = jsonObject.getString("errmsg");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return;
		}
		try {
			res = jsonObject.getString("data");
		} catch (JSONException e) {
			e.printStackTrace();
		}

		Gson gson = new Gson();
		java.lang.reflect.Type type = new TypeToken<Page<ExchangeRecord>>() {
		}.getType();
		Page<ExchangeRecord> page = gson.fromJson(res, type);
		hasMore = page.hasMore;

		if (page != null && page.records != null) {
			switch (postId) {
			case REFRESH:
				datas.clear();
				datas.addAll(page.records);
				break;
			case LOADMORE:
				datas.addAll(page.records);
				break;
			default:
				break;
			}

			if (datas.size() > 0) {
				cursorId = datas.get(datas.size() - 1).createdTime;
			} else {
				cursorId = null;
			}
			mAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onPostFailure(int postId, String msg) {
		super.onPostFailure(postId, msg);
	}

	@Override
	public void onLoadMore() {
		super.onLoadMore();
		RequestParams params = new RequestParams();
		params.addBodyParameter("itemId", getArguments().getString("itemId"));
		params.addBodyParameter("lastCursor", cursorId);
		onRequest(LOADMORE, UrlConstant.EXCHANGE_RECORD, HttpMethod.GET, params);
	}

	private class TouchListenerImpl implements OnTouchListener {
		@SuppressLint("ClickableViewAccessibility")
		@Override
		public boolean onTouch(View view, MotionEvent motionEvent) {
			switch (motionEvent.getAction()) {
			case MotionEvent.ACTION_DOWN:
				int scrollY1 = view.getScrollY();
				if (scrollY1 == 0) {
					Intent intent = new Intent("isTop");
					intent.putExtra("isTop", "isTop");
					getActivity().sendBroadcast(intent);
				} else {
					Intent intent = new Intent("isTop");
					intent.putExtra("isTop", "notTop");
					getActivity().sendBroadcast(intent);
				}

				break;
			case MotionEvent.ACTION_MOVE:
				int scrollY = view.getScrollY();
				int height = view.getHeight();
				int scrollViewMeasuredHeight = mScrollView.getChildAt(0).getMeasuredHeight();
				if (scrollY == 0) {
					Intent intent = new Intent("isTop");
					intent.putExtra("isTop", "isTop");
					getActivity().sendBroadcast(intent);
				} else {
					Intent intent = new Intent("isTop");
					intent.putExtra("isTop", "notTop");
					getActivity().sendBroadcast(intent);
				}
				if ((scrollY + height) == scrollViewMeasuredHeight) {
					Intent intent = new Intent("loadMore");
					getActivity().sendBroadcast(intent);
				}
				break;

			default:
				break;
			}
			return false;
		}

	};

}
